﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls.Primitives;
using Red.Controls.TimeLine.Core;
using Red.Controls.TimeLine.ViewModels;

namespace Red.Controls.TimeLine.TrackItemsControls
{
    public class MoveThumbBehavior : Thumb
    {
        static MoveThumbBehavior()
        {
            DefaultStyleKeyProperty.OverrideMetadata( typeof( MoveThumbBehavior ), new FrameworkPropertyMetadata( typeof( MoveThumbBehavior ) ) );
        }

        public MoveThumbBehavior()
        {
            DragDelta += new DragDeltaEventHandler( OnMoveThumbDragDeltaHandler );
        }

        private void OnMoveThumbDragDeltaHandler( object sender, DragDeltaEventArgs e )
        {
            Debug.Assert( DataContext != null );
            Debug.Assert( DataContext is TrackItemBase );

            TrackItemBase trackItem = (TrackItemBase)DataContext;
            TimeLineControler controler = trackItem.TimeLineControler;

            trackItem.ActionTime = trackItem.ActionTime + controler.ScreenOffsetToTimeOffset( e.HorizontalChange );

            e.Handled = true;
        }
    }

    public class ResizeThumbBehavior : Thumb
    {

        static ResizeThumbBehavior()
        {
            DefaultStyleKeyProperty.OverrideMetadata( typeof( ResizeThumbBehavior ), new FrameworkPropertyMetadata( typeof( ResizeThumbBehavior ) ) );
        }

        public ResizeThumbBehavior()
        {
            DragDelta += new DragDeltaEventHandler( ResizeThumb_DragDelta );
        }

        private void ResizeThumb_DragDelta( object sender, DragDeltaEventArgs e )
        {
            Debug.Assert( DataContext != null );
            Debug.Assert( DataContext is DurationTrackEvent );

            DurationTrackEvent durationTrackItem = (DurationTrackEvent)DataContext;
            TimeLineControler controler = durationTrackItem.TimeLineControler;

            double timeChange = controler.ScreenOffsetToTimeOffset( e.HorizontalChange );

            switch ( HorizontalAlignment )
            {
                case HorizontalAlignment.Left:
                    {
                        double newDuration = durationTrackItem.DurationTime - timeChange;
                        if ( newDuration < 0.0 )
                        {
                            durationTrackItem.ActionTime = durationTrackItem.EndTime;
                            durationTrackItem.DurationTime = 0.0;
                        }
                        else
                        {
                            durationTrackItem.ActionTime = durationTrackItem.ActionTime + timeChange;
                            durationTrackItem.DurationTime = durationTrackItem.DurationTime - timeChange;
                        }
                    }
                    break;
                case HorizontalAlignment.Right:
                    {
                        double newDuration = durationTrackItem.DurationTime + timeChange;
                        if ( newDuration < 0.0 )
                        {
                            durationTrackItem.DurationTime = 0.0;
                        }
                        else
                        {
                            durationTrackItem.DurationTime = durationTrackItem.DurationTime + timeChange;
                        }
                    }
                    break;
            }
            e.Handled = false;
        }
    }
}